/** 
 * 骰子区域组件
 * by wyy
*/
class diceGrp extends GameObject implements eui.UIComponent {
    public rollingBtn: eui.Rect;
    public rollingGrp0: eui.Group;
    public rollingGrp1: eui.Group;
    public resultGrp0: eui.Group;
    public resultGrp1: eui.Group;
    public resultGrp2: eui.Group;
    public resultGrp3: eui.Group;

    private isRolling: boolean;

    private round: number;
    private desk;

    constructor(round, desk) {
        super();
        this.skinName = "resource/eui_skins/game_skins/diceGrpSkin.exml";
        this.round = round;
        this.desk = desk;
    }
    firstCreate() {
        this.rollingBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.rolling, this);

        //显示玩家已投骰子
        nano.request("gamemanager.getmyselfinfo", {}, (data) => {
            globalObject.getInstance().player = data["data"];
            globalObject.getInstance().debugConsole("显示玩家已投骰子：", globalObject.getInstance().player);
            let diceNum = (<string>data.data.Result).split(",");
            if ((<string>data.data.Result) != "") {
                for (let v of diceNum) {
                    let dice = this.createDice(Number(v));
                    this.addChild(dice);
                    this.diceToResultGrp(dice);
                }
                if (diceNum.length >= 4) {
                    this.rollingBtn.fillColor = globalObject.getInstance().gameColor.lightGrey;
                }
            }
        });
    }
    addToStage() { }
    onRemove() { }
    private rolling() {
        if (this.isRolling) {
            return;
        }
        this.isRolling = true;
        this.rollingBtn.fillColor = globalObject.getInstance().gameColor.lightGrey;
        nano.request("gamemanager.rolldice", {}, (data) => {
            globalObject.getInstance().debugConsole("投掷结果", data);
            //投掷成功
            if (data.code == 200) {

                for (let i in data.data) {
                    let dice = this.createDice(data.data[i]);
                    this["rollingGrp" + i].addChild(dice);
                    dice.play(2);
                }
                if (Number(data.message) >= 2) {
                    this.rollingBtn.fillColor = globalObject.getInstance().gameColor.lightGrey;
                    return;
                }
            }
            else {
                this.rollingBtn.fillColor = globalObject.getInstance().gameColor.lightGrey;
                globalObject.getInstance().showTip(data.message);
                this.isRolling = false;
            }
        });
    }
    //创建骰子
    private createDice(num: number): egret.MovieClip {
        let dice: egret.MovieClip = new egret.MovieClip(globalObject.getInstance().diceFactory.generateMovieClipData(num.toString()));
        dice.addEventListener(egret.Event.COMPLETE, this.rollingEnd, this);

        return dice;
    }
    //骰子摇动结束后移到结果区
    private rollingEnd(evt) {
        let dice = evt.target;
        this.diceToResultGrp(dice);
        //更新自己的数据显示
        nano.request("gamemanager.getmyselfinfo", {}, (data) => {
            globalObject.getInstance().player = data.data;
            let id = globalObject.getInstance().player.Userid;
            this.desk.playerList[id].renewTime(globalObject.getInstance().player.Rolltime);
            this.desk.playerList[id].renewNum(globalObject.getInstance().player.Result);
            this.isRolling = false;
            if (globalObject.getInstance().player.Result.length <= 3) {
                this.rollingBtn.fillColor = globalObject.getInstance().gameColor.red;
            }
        });
    }
    //把骰子移到结果区
    private diceToResultGrp(dice) {
        for (let i = 0; i < 4; i++) {
            let s = "resultGrp" + (Number(i) + 1);
            if ((<eui.Group>this[s]).numChildren <= 0) {
                let p = (dice.parent).localToGlobal(dice.x, dice.y);
                let localP = (<eui.Group>this[s]).globalToLocal(p.x, p.y);
                dice.parent.removeChild(dice);
                dice.x = localP.x;
                dice.y = localP.y;
                (<eui.Group>this[s]).addChild(dice);
                egret.Tween.get(dice).to({
                    x: 0,
                    y: 0
                }, 300);
                return;
            }
        }
    }
}